<template>
  <div class="checkPlan_box">
    <div class="checkPlan_top">
      <div class="checkPlan_title">点检计划完成率</div>
      <div class="checkPlan_month">
        <el-select
          v-model="value"
          placeholder="请选择"
          :popper-append-to-body="false"
          @change="getValue"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
    </div>

    <div v-if="JSON.stringify(listData) !='[]'" class="echarts_container">
      <div id="map5" class="col-md-12  col-sm-12 col-xs-12" style="height: 100%;" />
      <div id="maps5" class="col-md-12  col-sm-12 col-xs-12" style="height: 100%; " />
    </div>
    <div v-else class="echarts_noData">
      <svg-icon icon-class="暂无数据" />
    </div>
  </div>
</template>

<script>
import { checkPlanPer } from '@/api/system/largeScreen'
import { mapGetters } from 'vuex'
export default {
  computed: {
    ...mapGetters([
      'windowResizeTag'
    ])
  },
  data() {
    return {
      listData: [],
      value: '1',
      options: [{
        value: '1',
        label: '面积图'
      }, {
        value: '2',
        label: '柱状图'
      }],
      myChart: undefined,
      vertical: undefined,
      timer: undefined
    }
  },
  watch: {
    windowResizeTag() {
      if (this.myChart || this.vertical) {
        this.$nextTick(() => {
          setTimeout(() => {
            this.drawLine()
          }, 500)
        })
      }
    }
  },
  mounted() {
    // this.drawLine()

    this.getCheckPlanPer()
    this.timer = setInterval(() => {
      this.getCheckPlanPer()
    }, this.$store.state.common.refreshFrequency * 1000)
  },
  created() {

  },
  beforeDestroy() {
    // 页面关闭清除定时器
    clearInterval(this.timer)
  },
  methods: {
    getCheckPlanPer() {
      checkPlanPer({ companyId: this.$store.state.user.companyId }).then(res => {
        this.listData = res.data
        this.$nextTick(() => {
          this.drawLine()
          this.getValue()
        })
      })
    },
    getValue() {
      const myChartline = document.getElementById('map5')
      const myVertical = document.getElementById('maps5')
      if (this.value == '1') {
        myChartline.style.display = 'block'
        myVertical.style.display = 'none'
      } else {
        myChartline.style.display = 'none'
        myVertical.style.display = 'block'
      }
      this.$nextTick(() => {
        this.drawLine()
      })
    },
    drawLine() {
      if (this.myChart) {
        this.$echarts.init(document.getElementById('map5')).dispose()
      }
      if (this.vertical) {
        this.$echarts.init(document.getElementById('maps5')).dispose()
      }
      this.myChart = this.$echarts.init(document.getElementById('map5'))
      this.vertical = this.$echarts.init(document.getElementById('maps5'))
      this.myChart.setOption({
        grid: {
          show: false,
          top: '20',
          right: '20',
          bottom: '40',
          left: '60'
        },
        xAxis: {
          type: 'category',
          data: this.listData.map(item => item.date),
          axisLine: {
            show: false, // 隐藏X轴轴线
            lineStyle: {
              color: '#999999'
            }
          },
          axisTick: {
            show: false
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            show: false, // 隐藏X轴轴线
            lineStyle: {
              color: '#999999'
            }
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            formatter: '{value}%',
            show: true,
            color: '#999999', // 更改坐标轴文字颜色
            textStyle: {
              fontSize: 12 // 更改坐标轴文字大小
            },
            margin: 20
          },
          splitLine: { show: true }
        },
        tooltip: {
          trigger: 'axis',
          padding: [12, 17, 20, 23],
          textStyle: { color: '#333' },
          formatter: function(params) {
            return params[0].name + '<br/>' +
                "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#9169FF'></span>" + '完成率: ' + params[0].value + '%'
          }
        },
        series: [
          {
            type: 'line',
            data: this.listData.map(item => item.completedRatio),
            smooth: true, // 平滑曲线
            showSymbol: false,
            itemStyle: { color: '#9169FF' },
            lineStyle: {
              width: 3,
              color: 'rgba(145, 105, 255, 1)',
              shadowColor: 'rgba(0, 0, 0, 0.3)',
              shadowOffsetX: 0, // 折线的X偏移
              shadowOffsetY: 6, // 折线的Y偏移
              shadowBlur: 6 // 折线模糊
            },
            areaStyle: { color: 'rgba(145, 105, 255, 0.2)' }
          }
        ]
      })
      this.vertical.setOption({
        tooltip: {
          trigger: 'axis',
          padding: [12, 17, 20, 23],
          textStyle: { color: '#333' },
          axisPointer: {
            type: 'shadow'
          },
          formatter: function(params) {
            return params[0].name + '<br/>' +
                "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:#5AD1FF'></span>" + '完成率: ' + params[0].value + '%'
          }
        },
        grid: {
          show: false,
          top: '20',
          right: '20',
          bottom: '40',
          left: '60'
        },
        color: ['#5AD1FF'],
        xAxis: {
          type: 'category',
          data: this.listData.map(item => item.date),
          axisTick: {
            show: false
          },
          offset: 2,
          axisLabel: {
            show: true,
            color: '#999999', // 更改坐标轴文字颜色
            textStyle: {
              fontSize: 12 // 更改坐标轴文字大小
            }
          },
          axisLine: {
            show: false
          }
        },
        yAxis: {
          type: 'value',
          // 影藏y轴刻度
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            formatter: '{value}%',
            show: true,
            color: '#999999', // 更改坐标轴文字颜色
            textStyle: {
              fontSize: 12 // 更改坐标轴文字大小
            },
            margin: 20
          }
        },
        series: [
          {
            data: this.listData.map(item => item.completedRatio),
            type: 'bar',
            itemStyle: {
              normal: {
                // color: '#FF9ADC',
                barBorderRadius: [20, 20, 0, 0]
                // shadowColor: 'rgba(0, 0, 0, 0.3)',
                // shadowOffsetX: 2, // 折线的X偏移
                // shadowOffsetY: 0, // 折线的Y偏移
                // shadowBlur: 6 // 折线模糊
              }
            },
            barWidth: 12 // 柱图宽度
          }
        ]
      })
    }
  }
}
</script>

<style lang="less" scoped>
.checkPlan_box{
  width: 100%;
  height: 100%;
}
.checkPlan_top {
  display: flex;
  justify-content: space-between;

  .checkPlan_title {
    font-size: 20px;
    font-weight: bold;
    line-height: 34px;
    letter-spacing: -0.21px;
    color: #333333;
    padding-top: 16px;
    padding-left: 20px;
  }

  .checkPlan_month{
    padding-top: 16px;
    padding-right: 20px;
  }
}
.echarts_container{
    padding: 0 10px;
    height: calc(100% - 50px);
}
</style>
